package com.zjff.mobiletwo.service;

import com.zjff.mobiletwo.entity.ProductEntity;
import com.zjff.mobiletwo.entity.ResultEntity;
import com.zjff.mobiletwo.dao.ProductDao;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {
    @Resource
    ProductDao ProductDao;
    private static final Logger logger = LoggerFactory.getLogger(ProductService.class);

    /**
     * 获取商品详情
     * @return JSON
     */
    public String getProductList(){
        try{
            return ResultEntity.Success(null,ProductDao.getProductList(),0,"获取成功!");
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return ResultEntity.Fail(-10007,"上传失败!");
        }
    }

    /**
     * 根据id条件查询商品分页信息
     * @return JSON
     */
    public String getCateList(){
        try{
            return ResultEntity.Success(null,ProductDao.getCateList(),0,"获取成功!");
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return ResultEntity.Fail(-10007,"上传失败!");
        }
    }

    /**
     * 通过商品id获取商品信息
     * @param id
     * @return JSON
     */
    public String getProductById(int id){
        try{
            return ResultEntity.Success(null,ProductDao.getProductById(id),0,"获取成功!");
        }catch(Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return ResultEntity.Fail(-10007,"上传失败!");

        }
    }

    /**
     * 根据id获取图片信息
     * @param id
     * @return JSON
     */
    public String getImgListById(int id){
        try{
            return ResultEntity.Success(null,ProductDao.getImgListById(id),0,"获取成功!");
        }catch(Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return com.zjff.mobiletwo.entity.ResultEntity.Fail(-10007,"上传失败!");

        }
    }

    /**
     * 根据familyname进行模糊查询
     * @param name 分类名
     * @return
     */
    public List<ProductEntity> findByName(String name){
        return ProductDao.findByName(name);
    }
    public String getList(){
        try{
            return ResultEntity.Success(null,ProductDao.getList(),0,"获取成功!");
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return ResultEntity.Fail(-10007,"上传失败!");
        }
    }

    /**
     * 通过familyid获取商品信息
     * @param familyid
     * @return JSON
     */
    public String getProductListByFamilId(int familyid){
        try{
            return ResultEntity.Success(null,ProductDao.getProductListByFamilId(familyid),0,"获取成功!");
        }catch(Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return ResultEntity.Fail(-10007,"上传失败!");
        }
    }
}
